import 'package:flutter/material.dart';
import 'package:wk_weather/myConfig/dimen_constant.dart';
import 'package:wk_weather/generated/json/mybean/WeatherModel.dart';
import 'package:wk_weather/views/widget/BlurRectWidget.dart';
import 'package:wk_weather/views/pages/home/SunSetRiseView.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class RealTimeDetailView extends StatelessWidget {
  final WeatherModelEntity entity;

  const RealTimeDetailView({  Key key,  @required this.entity, }) : super(key: key);

  @override
  Widget build(BuildContext context) {

    var t1 = BlurRectWidget(
      child:  _buildSunSetRiseWidget(),
    ) ;

    var t2 = BlurRectWidget(
      child:  _buildBottomWidget(),
    ) ;



    var t3 =  Column(
      children: [
        t1,
        SizedBox(
          height: 10,
        ),
        t2 ,
      ],
    ) ;

    return t3;
  }


  Widget _buildSunSetRiseWidget() {
    List<WeatherModelResultDailyAstro> astro = entity?.result?.daily?.astro;
    if (astro == null || astro.isEmpty) {
      return Container();
    }
    return SunSetRiseView(model: entity?.result?.daily?.astro[0]);
  }

  Widget _buildBottomWidget() {
    const defaultStr = "--";
    String apparentTemp = defaultStr; // 体感温度
    String humidity = defaultStr; // 相对湿度
    String pressure = defaultStr; // 气压
    String wind = defaultStr; // 风速，米制下是公里每小时
    String pm25 = defaultStr; // pm25，质量浓度值
    String ultraviolet = defaultStr; // # 紫外线指数及其自然语言描述

    if (entity != null && entity.result != null && entity.result.realtime != null) {
      var realtime = entity.result.realtime;

      if (realtime.apparentTemperature != null) {
        apparentTemp = "${realtime.apparentTemperature.toInt()}°";
      }

      if (realtime.humidity != null) {
        humidity = "${(realtime.humidity * 100).toInt()}%";
      }

      if (realtime.pressure != null) {
        pressure = "${realtime.pressure}hPa";
      }

      if (realtime.wind != null && realtime.wind.speed != null) {
        wind = "${realtime.wind.speed}km/h";
      }

      if (realtime.airQuality != null && realtime.airQuality.pm25 != null) {
        pm25 = "${realtime.airQuality.pm25}";
      }

      if (realtime.lifeIndex != null &&  realtime.lifeIndex.ultraviolet != null) {
        ultraviolet = "${realtime.lifeIndex.ultraviolet.index}";
      }
    }

    return Container(
      padding: EdgeInsets.symmetric(horizontal: 10),
      child: Column(
        children: [
          SizedBox(
            height: 10,
          ),
          Row(
            children: [
              _buildItem("体感温度", apparentTemp),
              _buildItem("相对湿度", humidity),
            ],
          ),
          SizedBox(
            height: 10,
          ),
          Row(
            children: [
              _buildItem("气压", pressure),
              _buildItem("风速", wind),
            ],
          ),

          SizedBox(
            height: 10,
          ),
          Row(
            children: [
              _buildItem("PM25", pm25),
              _buildItem("紫外线强度", ultraviolet),
            ],
          ),
          SizedBox(
            height: 10,
          ),
        ],
      ),
    );
  }

  Widget _buildItem(String title, String desc) {
    var width = (1.wp - DimenConstant.cardMarginStartEnd * 2 -20  ) / 2;

    var view1 = Container(
      width: width,
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            "$title $desc",
            style: TextStyle(color: Color(0x99ffffff), fontSize: 14),
          ),
        ],
      ),
    );


    return  view1 ;// Flexible
    // return  Expanded(
    //   child: view1 ,
    // ) ;
  }
}
